package com.example.teaknower.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.teaknower.R;
import com.example.teaknower.model.Tea;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;


public class RecycleViewAdapter extends BaseQuickAdapter<Tea, BaseViewHolder> {
    public RecycleViewAdapter(int layoutResId, @Nullable List<Tea> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Tea item) {
        helper.setText(R.id.text,item.getTea_title());
//        returnBitMap(helper, item.getWine_img());
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                try {
                    URL url = new URL(item.getTea_img());
                    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
                    return bitmap;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                helper.setImageBitmap(R.id.icon,bitmap);
            }
        }.execute();
    }


}
